<?php e($html->css('jquery.ui/jquery.ui.css', 'stylesheet', null, false)); ?>
<?php e($javascript->link('jquery/jquery.ui.interact.js', false)); ?>
<div class="postTerms index">
<h2><?php __('Manage PostTerms');?></h2>
<!-- Left Fat Section -->
<div class="widfats">
    <!-- Table Navigation -->
    <div class="tablenav">
        <div class="batchactions"></div>
        <?php echo $this->element('pager'); ?>
    </div>
    <!-- Table Content Area -->
    <table id="table_post_term" class="table fixed">
        <thead title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th width="5%" class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox" /></th>
                <th width="10%"><?php echo $paginator->sort('id');?></th>
                <th width="35%"><?php echo $paginator->sort('name');?></th>
                <th width="20%"><?php echo $paginator->sort('post_count');?></th>
                <th width="30%" class="actions"><?php __('Actions');?></th>
            </tr>
        </thead>
        <tfoot title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox" /></th>
                <th><?php echo $paginator->sort('id');?></th>
                <th><?php echo $paginator->sort('name');?></th>
                <th><?php echo $paginator->sort('post_count');?></th>
                <th class="actions"><?php __('Actions');?></th>
            </tr>
        </tfoot>
        <tbody class="sortable">
            <?php foreach ($postTerms as $i=>$postTerm):
                $class = $i % 2 == 0 ? ' class="altrow"' : ''; ?>
            <tr<?php echo $class;?>>
                <td class="checkbox"><input type="checkbox" class="checkbox" /></td>
                <td class="id"><?php echo $postTerm['PostTerm']['id']; ?></td>
                <td class="name"><?php echo $postTerm['PostTerm']['name']; ?></td>
                <td class="count"><?php echo $postTerm['PostTerm']['post_count']; ?></td>
                <td class="actions">
                    <?php echo $html->link(__('View', true), array('action' => 'view', $postTerm['PostTerm']['id'])); ?>
                    <?php if($user['group'] != 'student'): ?>
                        <?php echo $html->link(__('Edit', true), array('action' => 'edit', $postTerm['PostTerm']['id'])); ?>
                        <?php echo $html->link(__('Delete', true), array('action' => 'delete', $postTerm['PostTerm']['id'])); ?>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <p class="faq"><strong>栏目的展示顺序?</strong>上面列表中的栏目是按照在首页的展示顺序从前到后排列的, 如果您想调整, 鼠标拖动各个公告栏目名称那个单元格, 拖到想要的位置, 松手即可, 系统会自动保存您的更改</p>
    <!-- PostTerm View Section -->
    <div id="posts_by_term"></div>
</div>
<!-- Sidebar Section -->
<div class="sidebar">
    <!-- PostTerm Add Form -->
    <div id="widget_add_post_term" class="widget">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Add PostTerm');?></span></h3>
        <div class="content">
            <div class="postTerms form">
            <!-- PostTerm Add Form -->
            <?php e($form->create('PostTerm', array('action'=>'add'))); ?>
                <fieldset>
                    <?php e($form->input('name', array('label'=>__('PostTerm', true), 'class' => 'text input', 'autocomplete'=>'off'))); ?>
                    <?php e($form->submit(__('Add PostTerm', true), array('class' =>'submit attention'))); ?>
                </fieldset>
            <?php e($form->end()); ?>
            </div>
        </div>
    </div>
    <!-- PostTerm Edit Form -->
    <div id="widget_edit_post_term" class="widget">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Edit PostTerm');?></span></h3>
        <div class="content">
            <p class="faq"><?php __('There are no PostTerm to edit now'); ?></p>
        </div>
    </div>
    <!-- Quick Actions -->
    <div id="widget_quick_actions" class="widget">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Quick Actions');?></span></h3>
        <div class="content">
            <div class="actions">
                <ul>
                    <li><?php echo $html->link(__('List Posts', true), array('controller' => 'posts', 'action' => 'index')); ?> </li>
                    <li><?php echo $html->link(__('New Post', true), array('controller' => 'posts', 'action' => 'add')); ?> </li>
                    <br class="clear"/>
                </ul>
            </div>
        </div>
    </div>
</div>
</div>
<script type="text/javascript">
jQuery(function(){
    /* Validate PostTermAddForm */
    $.sme.data.post_term = false;
    $('#PostTermAddForm').submit(function(){
        if(!$(this).valid()){ return false; }
        if(!$.sme.status.post_term){
            $.sme.notify('error', '该栏目名已经存在, 不能添加');
            return false;
        }
    }).validate({
        success: function(label){
            $.sme.cbk.ajaxCheck.apply({
                label: label,
                input: label.prev('input'),
                id: 'PostTermName',
                model: 'PostTerm',
                control: 'post_term',
                fielden: 'name',
                fieldzh: '栏目名'
            });
        },
        rules: { 'data[PostTerm][name]': 'required' },
        messages: { 'data[PostTerm][name]': '栏目名称不能为空值' }
    });
    /* Validate PostTermEditForm */
    $('#PostTermEditForm').submit(function(){
        if(!$(this).valid()){ return false; }
        if(!$.sme.status.post_term){
            alert('该栏目名已经存在, 不能添加');
            return false;
        }
        var target = $('div#widget_edit_post_term div.content');
        $(this).ajaxSubmit({
            global: false,
            target: target,
            beforeSend: function(){
                target.append($.sme.option.span);
            }
        });
        return false;
    }).validate({
        success: function(label){
            $.sme.cbk.ajaxCheck.apply({
                label: label,
                input: label.prev('input'),
                id: 'PostTermName',
                model: 'PostTerm',
                control: 'post_term',
                fielden: 'name',
                fieldzh: '栏目名'
            });
        },
        rules: { 'data[PostTerm][name]': 'required' },
        messages: { 'data[PostTerm][name]': '栏目名称不能为空值' }
    });
    /* Bind PostTerm Edit/View/Delete callbacks */
    $('#table_post_term td.actions a[href*=edit]')
        .data('ajax', 'div#widget_edit_post_term div.content')
        .click($.sme.cbk.ajaxView);
    $('#table_post_term td.actions a[href*=view]')
        .data('ajax', 'div#posts_by_term')
        .click($.sme.cbk.ajaxView);
    $('#table_post_term td.actions a[href*=delete]')
        .click($.sme.cbk.ajaxDelete);
    /* Enable sorting orders for post category */
    $('tbody.sortable').sortable({
        axis: 'y',
        containment: 'parent',
        items: '> tr',
        placeholder: 'placeholder',
        forcePlaceholderSize: true,
        revert: true,
        helper: 'clone',
        handle: 'td.name',
        update: function(event,ui){
            var terms = [], terms = $('tbody.sortable td.id');
            $.sme.notify('load', '正在保存新的栏目顺序, 稍后');
            $.each(terms, function(index, item){
                data = $.sme.datas({id: $(item).text(), order: index}, 'PostTerm');
                $.log(data);
                $.ajax({
                    type: 'post',
                    data: data,
                    dataType: 'json',
                    global: false,
                    async: false,
                    url: $.sme.url.post_term('order'),
                    success: function(json){
                        if(index == terms.length-1){
                            $.sme.autoNotify(json);
                            if(json.status){
                                $('tbody.sortable tr:nth-child(2)').removeClass('altrow');
                                $('tbody.sortable tr:nth-child(1)').addClass('altrow');
                                //$('tbody.sortable tr:first').addClass('altrow');
                            }
                        }
                    }
                });
            });
        }
    });
    $('tbody.sortable').disableSelection();
});
</script>